package cn.genmer.test.security.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class MyCallable implements Callable {
    int i = 0;
    @Override
    public Object call() throws Exception {
        System.out.println("当前线程名称:" + Thread.currentThread().getName());
        Thread.sleep(10000);
        return "Callable的返回值" + i++;
    }

    public static void main(String[] args) {
        Callable callable = new MyCallable();
//        for (int i = 0; i < 10; i++) {
//            FutureTask task = new FutureTask(callable);
//            new Thread(task,"子线程"+ i).start();
//            try {
//                //获取子线程的返回值
//                System.out.println("子线程返回值："+task.get() + "\n");
//            }  catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        FutureTask task = new FutureTask(callable);
        new Thread(task,"子线程").start();
        try {
            //获取子线程的返回值
            System.out.println("子线程返回值："+task.get() + "\n");
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }
}
